<?php
  header('Content-Type: application/json;charset=utf-8');
  header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
  header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
  header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
  header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段

  require_once("conn.php");

  $video_type = $_POST['video_type'];
  $video_id = $_POST['video_id'];

  // 评论、回复数据
  // 用户名、评论内容、点赞数量、点踩数量、发布时间

  // 获取评论
  $sql = "select * from comments join user on comments.comment_user_id = user.user_id where video_type = '$video_type' and video_id = $video_id and parent_comment_id = 0";
  $res = mysqli_query($conn, $sql);
  if(mysqli_num_rows($res) > 0) {
    foreach($res as $row) {
      $comment_list[] = array(
        "comment_id"=>$row["comment_id"],
        "comment_username"=>$row["user_username"],
        "comment"=>$row["comment"],
        "good_count"=>$row["good_count"],
        "bad_count"=>$row["bad_count"],
        "comment_date"=>$row["date"],
      );
    }
  }

  if(!isset($comment_list) || count($comment_list) == 0)
    echo '{"data":[]}';
  else
    echo json_encode($comment_list);
?>